﻿#include "gofgame/jumpdown/layer/MessageBoxLayer.h"
#include "gofgame/jumpdown/Scene.h"
#include "gofgame/jumpdown/io/LocalInformation.h"
#include "gofgame/jumpdown/io/Score.h"
#include "gofgame/jumpdown/util/objective-c.h"
#include "gofgame/common/Ref.h"
#include "gofgame/jumpdown/ListScene.h"

using namespace gofgame::jumpdown::layer;
using namespace gofgame::jumpdown::io;
using namespace cocos2d::extension;
using namespace cocos2d;
using namespace gofgame::common;
using namespace gofgame::jumpdown::util;

const int EDITBOX_TAG = 100;

MessageBoxLayer::MessageBoxLayer(){
	m_username = "Player";
	m_level = 0;
	m_rank = -1;
	m_tipStr = "加油请重试";
}

void MessageBoxLayer::onEnter(){
	CCLayerColor::onEnter();
	//CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,-128-1,true);
}

void MessageBoxLayer::onExit() {
	CCLayerColor::onExit();
	CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}

bool MessageBoxLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){
	m_bTouchedMenu = m_pMenu->ccTouchBegan(pTouch,pEvent);
	return true;
}

void MessageBoxLayer::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
{  
	if (m_bTouchedMenu) {  
		m_pMenu->ccTouchMoved(pTouch, pEvent);  
	}  
}

void MessageBoxLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
{  
	if (m_bTouchedMenu) {  
		m_pMenu->ccTouchEnded(pTouch, pEvent);  
		m_bTouchedMenu = false;  
	}  
}  

void MessageBoxLayer::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
{  
	if (m_bTouchedMenu) {  
		m_pMenu->ccTouchEnded(pTouch, pEvent);  
		m_bTouchedMenu = false;  
	}  
}

bool MessageBoxLayer::init(){

	if (!this->initWithColor(ccc4(0,0,0,200))) {
		delete this;
		return false;
	}

	initData();
	initUI();

	return true;
}

void MessageBoxLayer::initData() {
	//获取分数
	Ref<Scene> scene = (Scene*)CCDirector::sharedDirector()->getRunningScene();
	this->m_level = (int)scene->getSpriteLayer()->getCurrentDepth();
	LocalInformation localInformation = LocalInformation::load();
	//获取用户名
	if(nullptr != localInformation.getPreviousName()){
		 this->m_username = localInformation.getPreviousName();
	}
	//获取预测的排名
	if (m_level) {
		m_rank = localInformation.getRank(m_level);
		if (m_rank != -1) {

			char str[200];
			sprintf(str,"恭喜你打破本地排行榜记录，你此次排名是 %d，点击保存按钮以保存记录",++m_rank);
			this->m_tipStr = str;
		}
	}

}

void MessageBoxLayer::editBoxReturn(CCEditBox* editBox){

}

void MessageBoxLayer::initUI() {

	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	this->setContentSize(CCSizeMake(winSize.width * 0.8f,winSize.height/2));
	CCSize dialogSize = this->getContentSize();
	this->setPosition(ccp(winSize.width/2 - dialogSize.width/2,winSize.height/2 - dialogSize.height/2));


	//YOU DIE
	CCLabelTTF *youdie = CCLabelTTF::create("你死了!", "", 70);
	youdie->setPosition(ccp(dialogSize.width/2,dialogSize.height*0.9));
	youdie->setColor(ccc3(255,0,0));
	this->addChild(youdie);
	//tip
	CCLabelTTF *tip = CCLabelTTF::create(this->m_tipStr.c_str(), "", 40);
	tip->setFontSize(20);
	tip->setDimensions(CCSizeMake(380,100));
	tip->setPosition(ccp(dialogSize.width/2,dialogSize.height*0.7));
	this->addChild(tip);
	//EDITBOX
	CCSize ebSize = CCSizeMake(200,40);
	CCEditBox *eb = CCEditBox::create(ebSize,CCScale9Sprite::create("editbuttion.jpg"));
	eb->setScaleY(1.2f);
	eb->setScaleX(0.8f);
	eb->setPosition(ccp(dialogSize.width*0.5 + 80,dialogSize.height*0.5));
	eb->setFontColor(ccRED);
	if(this->m_username.length() == 0){
		eb->setPlaceHolder("请输入你的名字:");
	} else {
		eb->setText(this->m_username.c_str());
	}
	eb->setFont("", 22);
	//eb->setPlaceHolder("please input your name");
	//eb->setText(this->m_username.c_str());
	eb->setMaxLength(10);
	eb->setReturnType(kKeyboardReturnTypeDone);
	eb->setDelegate(this);
	eb->setInputMode(kEditBoxInputModeSingleLine);
	eb->setVisible(false);
	eb->setTag(EDITBOX_TAG);
	this->addChild(eb);

	//randk
	//if() 
	CCMenuItemFont *mifOk = CCMenuItemFont::create("保存",this,menu_selector(MessageBoxLayer::okMenuItemCallBack));
	mifOk->setVisible(false);

	if(this->m_rank >= 0) {
		CCLabelTTF *rank = CCLabelTTF::create("请输入您的名字", "", 20);
		rank->setPosition(ccp(dialogSize.width*0.5-100,dialogSize.height*0.5));
		this->addChild(rank);
		eb->setVisible(true);
		mifOk->setVisible(true);
	}
	//menu
	CCMenuItemFont *mif = CCMenuItemFont::create("重新开始",this,menu_selector(MessageBoxLayer::restartMenuItemCallback));
	CCMenuItemFont *mif2 = CCMenuItemFont::create("排行榜",this,menu_selector(MessageBoxLayer::listMenuItemCallback));
	m_pMenu = CCMenu::create(mifOk,mif, mif2, nullptr);
	m_pMenu->alignItemsHorizontallyWithPadding(20);
	m_pMenu->setPosition(ccp(dialogSize.width * 0.5F , dialogSize.height*0.2F));

	this->addChild(m_pMenu);
}


MessageBoxLayer *MessageBoxLayer::createMessageBoxLayer() {
	MessageBoxLayer * mb = new MessageBoxLayer();
	mb->init();
	mb->autorelease();
	return mb;
}


void MessageBoxLayer::okMenuItemCallBack(cocos2d::CCObject *pSender){
	if (m_rank != -1) {
		CCEditBox *eb = static_cast<CCEditBox*>(this->getChildByTag(EDITBOX_TAG));
		LocalInformation localInformation = LocalInformation::load();
		localInformation.addScore(Score(eb->getText(), m_level));
		LocalInformation::store(localInformation);
		
		CCMessageBox("保存成功","提示");
	}
};  

void MessageBoxLayer::restartMenuItemCallback(cocos2d::CCObject *pSender){
	Ref<Scene> scene;
	oc_new<Scene>(&scene);
	CCDirector::sharedDirector()->replaceScene(scene);
	CCDirector::sharedDirector()->resume();
};  
void MessageBoxLayer::listMenuItemCallback(cocos2d::CCObject *pSender){
	ListScene* ls = ListScene::createListScene();
	CCDirector::sharedDirector()->pushScene(ls);	
}; 